package com.zhixin.zhixinmall.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.jd.open.api.sdk.DefaultJdClient;
import com.jd.open.api.sdk.JdClient;
import com.jd.open.api.sdk.domain.kplunion.GoodsService.request.query.JFGoodsReq;
import com.jd.open.api.sdk.domain.kplunion.GoodsService.response.query.JFGoodsResp;
import com.jd.open.api.sdk.request.kplunion.UnionOpenGoodsJingfenQueryRequest;
import com.jd.open.api.sdk.response.kplunion.UnionOpenGoodsJingfenQueryResponse;
import com.zhixin.zhixinmall.exception.ZhixinMallException;
import com.zhixin.zhixinmall.properties.JdProperties;
import com.zhixin.zhixinmall.service.JdService;

/**
 * @author cyc
 * @date 2023/10/09 08:10:41
 */
@Service
public class JdServiceImpl implements JdService {

    @Autowired
    private JdProperties jdProperties;

    /**
     * 调用京粉商品查询接口
     *
     * @param eliteId
     */
    @Override
    public JSONArray executeJingfenQuery(Integer eliteId,
            Integer pageIndex, Integer pageSize,
            String sortName, String sort) {
        try {
            UnionOpenGoodsJingfenQueryResponse response = executeJdClient(eliteId,
                    null, null,
                    pageIndex, pageSize,
                    sortName, sort);
            if (response.getQueryResult().getCode() == 202) {
                return null;
            }
            return parseResponse2JsonArray(response);
        } catch (Exception e) {
            throw new ZhixinMallException(e.getMessage());
        }
    }

    @Override
    public JSONArray executeJingfenGroupQuery(Long groupId, Long ownerUnionId,
            Integer pageIndex, Integer pageSize,
            String sortName, String sort) {
        try {
            UnionOpenGoodsJingfenQueryResponse response = executeJdClient(1001, groupId, ownerUnionId,
                    pageIndex, pageSize, sortName, sort);
            if (response.getQueryResult().getCode() == 202) {
                return null;
            }
            return parseResponse2JsonArray(response);
        } catch (Exception e) {
            throw new ZhixinMallException(e.getMessage());
        }
    }

    UnionOpenGoodsJingfenQueryResponse executeJdClient(Integer eliteId,
            Long groupId, Long ownerUnionId,
            Integer pageIndex, Integer pageSize,
            String sortName, String sort)
            throws Exception {
        JdClient client = new DefaultJdClient(jdProperties.getServerUrl(), "", jdProperties.getAppKey(),
                jdProperties.getAppSecret());
        UnionOpenGoodsJingfenQueryRequest request = new UnionOpenGoodsJingfenQueryRequest();
        JFGoodsReq goodsReq = new JFGoodsReq();
        request.setGoodsReq(goodsReq);
        goodsReq.setEliteId(eliteId);
        if (groupId != null && ownerUnionId != null) {
            goodsReq.setGroupId(groupId);
            goodsReq.setOwnerUnionId(ownerUnionId);
        }
        if (pageIndex != null) {
            goodsReq.setPageIndex(pageIndex);
        }
        if (pageSize != null) {
            goodsReq.setPageSize(pageSize);
        }
        if (sortName != null) {
            goodsReq.setSortName(sortName);
        }
        if (sort != null) {
            goodsReq.setSort(sort);
        }
        request.setVersion("1.0");
        return client.execute(request);
    }

    JSONArray parseResponse2JsonArray(UnionOpenGoodsJingfenQueryResponse response) {
        JSONObject parseObject = JSONObject.parseObject(response.getMsg());
        JSONObject jingfenQueryResponce = JSONObject.parseObject(parseObject
                .get("jd_union_open_goods_jingfen_query_responce").toString());
        return JSONObject
                .parseObject(jingfenQueryResponce
                        .get("queryResult").toString())
                .getJSONArray("data");
    }
}
